package com.hsr.web.controller;


import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.hsr.web.config.ServerConfig;
import com.hsr.web.dto.FileDTO;
import com.hsr.web.dto.FileQuery;
import com.hsr.web.entity.TFile;
import com.hsr.web.enums.ResultCode;
import com.hsr.web.service.ITFileService;
import com.hsr.web.utils.FileUtil;
import com.hsr.web.utils.ResultVOUtil;
import com.hsr.web.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Optional;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tb
 * @since 2022-04-05
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/file")
@Slf4j
public class TFileController {

    @Autowired
    private ITFileService fileService;
    @Autowired
    ServerConfig serverConfig;

    @PostMapping("/list")
    @ApiOperation("查询(分页)")
    public ResultVO list(@RequestBody FileQuery query) {
        return ResultVOUtil.success(fileService.getList(query));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public ResultVO delete(@PathVariable String id){
        fileService.removeById(id);
        return ResultVOUtil.success();
    }

    @SneakyThrows
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public ResultVO upload(@RequestParam("file") MultipartFile multipartFile,@RequestParam("desc") String desc){
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid+multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        String filePath = FileUtil.fileUpload(multipartFile, "/apps/file/",fileName);
        TFile file=new TFile();
        file.setFileUrl(filePath);
        file.setFileDesc(desc);
        file.setCreateTime(LocalDateTime.now());
        file.setFileName(multipartFile.getOriginalFilename());
        fileService.save(file);
        return ResultVOUtil.success(serverConfig.getUrl()+filePath);
    }

    @ApiOperation("获取文件")
    @GetMapping("/read")
    public void read(@RequestParam("path") String path, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try{
            File file = new File(path);
            Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(file.getName());
            response.setContentType(mediaType.orElse(MediaType.APPLICATION_OCTET_STREAM).toString());
            FileCopyUtils.copy(new FileInputStream(path), response.getOutputStream());
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }


}
